<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>客户列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">

    <style>
        .layui-table-cell {
            /* height: 60px;
      line-height: 60px; */
            height: 100px;
            white-space: normal;
            padding: 0px;
        }

        th .layui-table-cell {
            height: 30px;
            line-height: 30px;
        }

        th .laytable-cell-1-0-7 {
            height: 30px;
            line-height: 30px;
        }

        .layui-table img {
            width: 80px;
            height: 80px;
            object-fit: cover;
        }
    </style>
</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form id="fm1" class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">客户ID</label>
                                <div class="layui-input-inline">
                                    <input typej="text" name="gid" placeholder="请输入客户ID" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label">姓名</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="gname" placeholder="请输入客户姓名" class="layui-input"></input>
                                </div>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label">电话</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="gphone" placeholder="请输入电话号码" class="layui-input"></input>
                                </div>
                            </div>

                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="searchbtn">
                                    <i class="layui-icon"></i>搜索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">添加</button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="deletes">删除</button>
                </div>
            </script>

            <table class="layui-hide" id="currentTableFilter" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">修改</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            </script>
        </div>
    </div>
    <script type="text/html" id="picTpl">
        <img src="{{d.gpic || ''}}" name="gpic" alt="照片">
    </script>

    <script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>

    <script>
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;

            table.render({
                elem: '#currentTableFilter',
                url: 'http://localhost:8080/guest/pageData',
                toolbar: '#toolbarDemo',

                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [[
                    { type: "checkbox", fixed: 'left' },
                    { field: 'gid', title: '客户ID', sort: true, width: 100 },
                    { field: 'gpic', title: '客户照片', templet: '#picTpl', width: 120, width: 100 },
                    { field: 'gname', title: '姓名' },
                    { field: 'ggender', title: '性别' },
                    { field: 'gphone', title: '电话' },
                    { field: 'gmoney', title: '余额', sort: true },
                    { field: 'gintegral', title: '积分', sort: true },

                    { title: '操作', toolbar: '#currentTableBar', fixed: "right", width: 120 }
                ]],
                limits: [10, 15, 20, 25, 50],
                limit: 10,
                page: true,
                skin: 'line'
            });

            var data;
            // 监听搜索操作
            form.on('submit(searchbtn)', function (data) {
                var result = JSON.stringify(data.field);
                layer.alert(result, {
                    title: '最终的搜索信息'
                });

                //执行搜索重载
                table.reload('currentTableFilter', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        "gid": data.field.gid,
                        "gname": data.field.gname,
                        "gphone": data.field.gphone
                    }
                }, 'data');

                return false;
            });

            /**
             * 监听checkbox
             */
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加客户',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['30%', '60%'],
                        content: './guestadd.html',
                        end: function (index) {
                            table.reload('currentTableFilter');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'deletes') {  // 监听删除操作
                    layer.confirm('真的删除所选吗', function (index) {
                        var checkStatus = table.checkStatus(obj.config.id);
                        //获取选中数量
                        var selectCount = checkStatus.data.length;
                        if (selectCount == 0) {
                            layer.msg('至少选一个吧', function () { });
                            return false;
                        }
                        layer.confirm('真的要删除选中的项吗？', function (index) {
                            layer.close(index);
                            index = layer.load(1, { shade: [0.1, '#fff'] });

                            var isStr = "";
                            for (var i = 0; i < selectCount; i++) {
                                isStr = isStr + "," + checkStatus.data[i].gid;
                            }
                            $.ajax({
                                data: { "gids": isStr },
                                url: 'http://localhost:8080/guest/deletes',
                                success: function (data) {
                                    if (data.code == 0) {
                                        $('#reload').click();
                                    }
                                    layer.close(index);
                                    layer.msg('删除成功！');
                                    table.reload('currentTableFilter', {});
                                }, erro: function (code) {
                                    layer.msg('操作失败！');
                                }
                            })
                        })
                    });
                } else if (obj.event === 'export') {  // 监听导出操作
                    location.href = "http://localhost:8080/guest/delete?" + $("#fm1").serialize();
                }
            });


            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                console.log(obj.event);

                if (obj.event === 'edit') {
                    var s = JSON.stringify(data);
                    localStorage.setItem("guest", JSON.stringify(data));

                    var index = layer.open({
                        title: '编辑客户信息',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['30%', '60%'],
                        content: './guestedit.html',
                        end: function (index) {
                            table.reload('currentTableFilter');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('确认删除吗？', function (index) {

                        $.ajax({
                            "url": "http://localhost:8080/guest/delete",
                            "data": {
                                "gid": data.gid
                            },
                            "success": function (result) {
                                if (result.code == 200) {
                                    layer.msg("删除成功");
                                    table.reload('currentTableFilter', {});
                                } else {
                                    layer.msg("删除失败");
                                }
                            }
                        });

                        layer.close(index);
                    });
                }
            });

        });
    </script>
</body>

</html>